﻿namespace UFIDA.U9.CUST.AITBarCodeBP
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using UFIDA.U9.CUST.AITBarCodeBE;
    using UFIDA.U9.Lot;
    using UFIDA.U9.MO.MO;
    using UFIDA.U9.PM.Rcv;
    using UFIDA.U9.QC.QCDocBE;
    using UFIDA.U9.SM.Ship;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// CreateBarCode partial 
	/// </summary>	
	public partial class CreateBarCode 
	{	
		internal BaseStrategy Select()
		{
			return new CreateBarCodeImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CreateBarCodeImpementStrategy : BaseStrategy
	{
		public CreateBarCodeImpementStrategy() { }

		public override object Do(object obj)
		{						
			CreateBarCode bpObj = (CreateBarCode)obj;

			List<long> retList = new List<long>();
			if (string.IsNullOrEmpty(bpObj.EntityType)) throw new Exception("EntityType为空");
            if (bpObj.IDS == null || bpObj.IDS.Count == 0) return null;

            using (ISession session = Session.Open())
			{
				if (bpObj.EntityType == "MOIDS")
                {
                    retList=FromMO(bpObj.IDS);
                }
                else if (bpObj.EntityType == "QCIDS")
                {
                    retList = FromQC(bpObj.IDS);
                }
                else if (bpObj.EntityType == "ShipIDS")
                {
                    retList = FromShip(bpObj.IDS);
                }
                else if (bpObj.EntityType == "RCVIDS")
                {
                    retList = FromRCV(bpObj.IDS);
                }
                else
				{
					throw new Exception($"EntityType[{bpObj.EntityType}]传入错误");
				}
				session.Commit();
            }
			return retList;
		}

        private static List<long> FromRCV(List<long> ids)
        {
            var retList = new List<long>();
            foreach (var id in ids)
            {
                RcvLine docline = RcvLine.Finder.FindByID(id);
                if (docline == null) throw new Exception("依据ID查找收货单行失败!");
                AITBarCode be = AITBarCode.Create();
                be.ItemKey = docline.ItemInfo.ItemID.Key;
                be.UOMKey = docline.TradeUOMKey;
                be.Qty = docline.RcvQtyTU;
                be.SrcDoc = new Base.PropertyTypes.BizEntityKey(docline.Receivement.Key.ID, docline.Receivement.Key.EntityType);
                be.SrcDocLine = new Base.PropertyTypes.BizEntityKey(docline.Key.ID, docline.Key.EntityType);
                be.SrcDocNo = docline.Receivement.DocNo;
                be.SrcDocLineNo = docline.DocLineNo;
                be.DescFlexSegments = docline.DescFlexSegments;
                be.ProjectKey = docline.ProjectKey;

                //批号
                if (docline.InvLotKey != null)
                {
                    be.LotMaster = docline.InvLotKey.GetEntity();
                    be.LotCode = be.LotMaster.Code;
                }

                retList.Add(be.ID);
            }
            return retList;
        }

        private static List<long> FromShip(List<long> ids)
        {
            List<long> retList = new List<long>();
            foreach (var id in ids)
            {
                ShipLine docline = ShipLine.Finder.FindByID(id);
                if (docline == null) throw new Exception("依据ID查找出货单行失败!");
                AITBarCode be = AITBarCode.Create();
                be.ItemKey = docline.ItemInfo.ItemID.Key;
                be.UOMKey = docline.InvUomKey;
                be.Qty = docline.ShipQtyTUAmount;
                be.SrcDoc = new Base.PropertyTypes.BizEntityKey(docline.Ship.Key.ID, docline.Ship.Key.EntityType);
                be.SrcDocLine = new Base.PropertyTypes.BizEntityKey(docline.Key.ID, docline.Key.EntityType);
                be.SrcDocNo = docline.Ship.DocNo;
                be.SrcDocLineNo = docline.DocLineNo;
                be.DescFlexSegments = docline.DescFlexField;
                be.ProjectKey = docline.ProjectKey;

                //批号
                if (docline.LotInfo?.LotMaster != null)
                {
                    be.LotMaster = docline.LotInfo?.LotMaster;
                    be.LotCode = docline.LotInfo?.LotMaster.Code;
                }

                retList.Add(be.ID);
            }

            return retList;
        }

        private static List<long> FromQC(List<long> ids)
        {
            List<long> retlist = new List<long>();
            foreach (var id in ids)
            {
                QCDocResult resline = QCDocResult.Finder.FindByID(id);
                if (resline == null) throw new Exception("依据ID查找检验结果行失败!");
                QCDocLine docline = resline.QCDocLine;
                AITBarCode be = AITBarCode.Create();
                be.ItemKey = docline.ItemMasterKey;
                be.UOMKey = docline.UOMKey;
                if (resline.Count == 0) throw new Exception("结果行的数量为0");
                be.Qty = resline.Count;
                be.SrcDoc = new Base.PropertyTypes.BizEntityKey(docline.QCDoc.Key.ID, docline.QCDoc.Key.EntityType);
                be.SrcDocLine = new Base.PropertyTypes.BizEntityKey(docline.Key.ID, docline.Key.EntityType);
                be.SrcDocNo = docline.QCDoc.DocNo;
                be.SrcDocLineNo = docline.QCDocLineNo;
                be.DescFlexSegments = docline.DescFlexField;
                be.ProjectKey = docline.QCProjectKey;
                be.Grade = resline.Grade;
                be.Element = resline.Element;
                //批号
                if (docline.LotInfo?.LotMaster?.EntityID > 0)
                {
                    LotMaster lot = LotMaster.Finder.FindByID(docline.LotInfo?.LotMaster?.EntityID);
                    if (lot == null) throw new Exception("查找批号主档失败!");
                    be.LotMaster = lot;
                    be.LotCode = docline.LotInfo.LotCode;
                }

                retlist.Add(be.ID);
            }
            return retlist;
        }

        private static List<long> FromMO(List<long> ids)
        {
            List<long> retlist=new List<long>();

            foreach (var id in ids)
            {
                MO doc = MO.Finder.FindByID(id);
                if (doc == null) throw new Exception("依据ID查找MO失败!");
                AITBarCode be = AITBarCode.Create();
                be.ItemKey = doc.ItemMasterKey;
                be.UOMKey = doc.ProductUOMKey;
                be.Qty = doc.ProductQtybyStoreUOM;
                be.SrcDoc = new Base.PropertyTypes.BizEntityKey(doc.Key.ID, doc.Key.EntityType);
                be.SrcDocNo = doc.DocNo;
                be.DescFlexSegments = doc.DescFlexField;
                be.ProjectKey = doc.ProjectKey;
                be.Grade = doc.DemandGrade;
                //批号
                if (doc.ProductLotMaster > 0)
                {
                    LotMaster lot = LotMaster.Finder.FindByID(doc.ProductLotMaster);
                    if (lot == null) throw new Exception("查找批号主档失败!");
                    be.LotMaster = lot;
                    be.LotCode = doc.ProductLotNo;
                }

                retlist.Add(be.ID);
            }

            return retlist;
        }
    }

	#endregion
	
	
}